<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Offline behavior</title>
</head>
<body>

<script src="https://unpkg.com/socket.io-client@4.1.2/dist/socket.io.js"></script>
<script>
  /*
    By default, any event emitted while the Socket is not connected will be buffered until reconnection.
    默认情况下，在 Socket 未连接时发出的任何事件都将被缓冲，直到重新连接。

    While useful in most cases (when the reconnection delay is short), it could result in a huge spike of events when the connection is restored.
    虽然在大多数情况下很有用（当重新连接延迟很短时），但它可能会在连接恢复时导致大量事件。

    There are several solutions to prevent this behavior, depending on your use case:
    有几种解决方案可以防止这种行为，具体取决于您的用例：
  */

  const socket = io("ws://localhost:3000");


  //法① use the connected attribute of the Socket instance
  if (socket.connected) {
    socket.emit( /* ... */ );
  } else {
    // ...
  }

  //法② use volatile events
  //如果此时发射, 但服务器没有连接上, 该事件是会被丢弃的,
  //而不会像默认.emit一样被缓存, 然后在reconnection时重发
  socket.volatile.emit( /* ... */ );

  //法③ empty the internal buffer upon reconnection
  //重连时 手动情况缓存 也可以解决此问题
  socket.on("connect", () => {
    socket.sendBuffer = [];
  });
</script>
</body>
</html>
